package com.xinqi.modules.chat.chat.friend.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.chat.chat.friend.domain.ChatFriendEntity;
import com.xinqi.modules.chat.chat.friend.dto.req.ChatFriendListQueryDTO;
import com.xinqi.modules.chat.chat.friend.dto.rsp.*;
import com.xinqi.modules.chat.chat.friend.manager.ChatFriendManager;
import com.xinqi.modules.chat.chat.friend.mapper.ChatFriendMapper;
import com.xinqi.modules.chat.chat.group.dto.req.ConversationSelectDTO;
import com.xinqi.modules.chat.chat.group.dto.req.GroupUserFriendQueryDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ConversationResultDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.GroupUserFriendResultDTO;
import com.xinqi.modules.chat.enums.friend.FriendStatusEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 好友列表管理层实现
 *
 * @author: system
 * @date: 2023/01/12
 */
@Service
@RequiredArgsConstructor
public class ChatFriendManagerImpl extends BaseServiceImpl<ChatFriendMapper, ChatFriendEntity> implements ChatFriendManager {

    /**
     * 获取好友列表集合
     *
     * @param query ChatFriendListQueryDTO
     * @return
     */
    @Override
    public List<ChatFriendDetailsResultDTO> findFriendList(ChatFriendListQueryDTO query) {
        return this.baseMapper.findFriendList(query);
    }

    /**
     * 获取好友详情
     *
     * @param userId   用户Id
     * @param friendId 好友Id
     * @return
     */
    @Override
    public ChatFriendDetailsResultDTO findFriendDetail(Long userId, Long friendId) {
        return this.baseMapper.findFriendDetail(userId, friendId);
    }

    /**
     * 通过用户id和好友id查询详情
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public ChatFriendEntity friedByUserIdFriendId(Long userId, Long friendId) {
        LambdaQueryWrapper<ChatFriendEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatFriendEntity::getUserId, userId)
            .like(ChatFriendEntity::getFriendId, friendId)
            .last("LIMIT 1");
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 黑名单列表
     *
     * @Title:
     * @MethodName: blacklist
     * [query]
     * @Return java.util.List<com.xinqi.modules.chat.chat.friend.dto.rsp.ChatFriendBlackListDto>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/10 10:29
     */
    @Override
    public List<ChatFriendBlackListDto> blacklist(ChatFriendBlackListDto query) {
        return this.baseMapper.blacklist(query);
    }

    /**
     * 好友汇总列表只包含头像名称和会话ID
     *
     * @param dto
     * @return
     */
    @Override
    public List<ConversationResultDTO> selectConversation(ConversationSelectDTO dto) {
        return this.baseMapper.selectConversation(dto);
    }

    /**
     * 查询非群员的好友
     *
     * @param dto
     * @return
     */
    @Override
    public List<GroupUserFriendResultDTO> groupUserFriend(GroupUserFriendQueryDTO dto) {
        return this.baseMapper.groupUserFriend(dto);
    }

    /**
     * 查询用户的黑名单数量
     *
     * @param userId
     * @return
     */
    @Override
    public Long selectCountBlack(Long userId) {
        LambdaQueryWrapper<ChatFriendEntity> wrapper = Queries.lambda();
        wrapper.eq(ChatFriendEntity::getUserId, userId)
            .eq(ChatFriendEntity::getStatus, FriendStatusEnum.TWO.getCode());
        return this.baseMapper.selectCount(wrapper);
    }

    /**
     * 查询用户的好友数量
     *
     * @param userId
     * @return
     */
    @Override
    public Long selectCountFriend(Long userId) {
        LambdaQueryWrapper<ChatFriendEntity> wrapper = Queries.lambda();
        wrapper.eq(ChatFriendEntity::getUserId, userId)
            .eq(ChatFriendEntity::getIsFriend, YesNoNumberEnum.YES.getCode())
            .eq(ChatFriendEntity::getStatus, FriendStatusEnum.ONE.getCode());
        return this.baseMapper.selectCount(wrapper);
    }

    /**
     * 获取用户信息，与是否是好友
     *
     * @param dto
     * @return
     */
    @Override
    public List<ChatFriendInfoResultDTO> userInfoByUserId(ChatFriendListQueryDTO dto) {
        return this.baseMapper.userInfoByUserId(dto);
    }

    /**
     * 检查是否是好友
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public ChatFriendResultDTO friedCheck(Long userId, Long friendId) {
        ChatFriendResultDTO result = this.baseMapper.friedCheck(userId, friendId);
        if (Objects.nonNull(result)) {
            result.setUserId(userId);
            result.setFriendId(friendId);
        }
        return result;
    }

    /**
     * 校验助手已绑定当前正用户信息
     */
    @Override
    public FriendAssistantCurrentDTO checkCurrentAssistant() {
        return this.baseMapper.checkCurrentAssistant();
    }

}
